package com.armrest.screen.provider

import android.widget.*
import com.armrest.screen.HomeMenuModel
import com.armrest.screen.ItemViewType
import com.armrest.screen.R
import com.armrest.screen.setMediaPlayer
import com.chad.library.adapter.base.provider.BaseItemProvider
import com.chad.library.adapter.base.viewholder.BaseViewHolder

class VolumeProvider(
    var layout: Int = R.layout.sound_home_layout, var itemViewTypes:
    Int = ItemViewType.ITEM_TYPE_GRAVITY_MODEL
) : BaseItemProvider<HomeMenuModel>() {
    override val itemViewType: Int
        get() = itemViewTypes
    override val layoutId: Int
        get() = layout
    var itemClick: ((position: Int) -> Unit)? = null
    var volume: Int = 0
    private var volumeBoolean: Boolean = true
    override fun convert(helper: BaseViewHolder, item: HomeMenuModel) {
        val volumeRl = helper.getViewOrNull<RelativeLayout>(R.id.volume_ll)
        val volumeImg = helper.getViewOrNull<ImageView>(R.id.volume_img)
        val volumeOff = helper.getViewOrNull<TextView>(R.id.volume_off)
        val progressBar = helper.getViewOrNull<ProgressBar>(R.id.progressBar)
        volumeRl?.let {
            volumeRl.setOnClickListener {
                it.setMediaPlayer()
                if(volumeBoolean){
                    if(100 == volume ||50 == volume ||25 == volume || 0 == volume){
                        volume += if(50 == volume  ){
                            50 }else{ 25 }
                    }
                    if(100 == volume){
                        volumeBoolean = false
                    }
                }else if(100 == volume || 50 == volume ||25 == volume || 0 == volume){
                    volume -= if(100 == volume  ){
                        50 }else{ 25 }
                    volumeBoolean = 0 == volume
                }
                volumeOff?.text=if(0 == volume) "已关闭" else "$volume%"
                 val icon= when (volume) {
                     0 -> R.mipmap.icon_volume_off
                     25 -> R.mipmap.icon_volume_one
                     50 -> R.mipmap.iicon_volume_two
                     else -> R.mipmap.icon_volume_max
                 }
                volumeImg?.setImageResource(icon)
                progressBar?.progress=volume
                itemClick?.invoke(volume)
            }
        }
    }
}